package com.example.a517;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class ServiceTest extends AppCompatActivity implements View.OnClickListener {
    private MyService.DownloadBinder downloadBinder;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {  // 活动成功绑定的时候调用
            downloadBinder = (MyService.DownloadBinder) service; // 活动成功断开的时候调用
            downloadBinder.startaDownoad();
            downloadBinder.getprogress();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    private Button btn_start;
    private Button btn_end;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_service_test);

        btn_start = findViewById(R.id.btn_start);
        btn_end = findViewById(R.id.btn_end);

        btn_end.setOnClickListener(this);
        btn_start.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
            if (v.getId() == R.id.btn_end){
                //Intent stopIntent = new Intent(this,MyService.class);
                //stopService(stopIntent);
                unbindService(connection);
            }else if (v.getId() == R.id.btn_start){
               // Intent startIntent = new Intent(this,MyService.class);
               // startService(startIntent);
                Intent intent = new Intent(this, MyService.class);
                bindService(intent,connection,BIND_AUTO_CREATE); // 绑定服务
            }
    }
}